package com.shbaiche.bifenba.ui.video;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.shbaiche.bifenba.App;
import com.shbaiche.bifenba.R;
import com.shbaiche.bifenba.model.base.TransMessage;
import com.shbaiche.bifenba.model.user.LoginMessage;
import com.shbaiche.bifenba.model.video.ArticleGroupListMessage;
import com.shbaiche.bifenba.model.video.ArticleGroupVo;
import com.shbaiche.bifenba.net.JsonCallback;
import com.shbaiche.bifenba.net.NetApi;
import com.shbaiche.bifenba.ui.base.BaseFragment;
import com.shbaiche.bifenba.ui.home.CategoryTabStrip;
import com.shbaiche.bifenba.ui.home.SearchActivity;
import com.shbaiche.bifenba.util.StringUtil;
import com.shbaiche.bifenba.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.droidlover.xdroidbase.base.XFragmentAdapter;
import cn.droidlover.xstatecontroller.XStateController;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;
import okhttp3.Call;


public class VideoFragment extends BaseFragment {
    @BindView(R.id.category_strip)
    CategoryTabStrip tabs;

    @BindView(R.id.view_pager)
    ViewPager pager;

    @BindView(R.id.contentLayout)
    XStateController contentLayout;

    @BindView(R.id.video_title_edit)
    TextView mEdt;

    List<Fragment> fragmentList = new ArrayList<>();
    XFragmentAdapter adapter;

    public VideoFragment(){}

    public static VideoFragment newInstance() {
        return new VideoFragment();
    }


    @Override
    public void initData(Bundle savedInstanceState) {
        tabs.type = 2;

        contentLayout.loadingView(View.inflate(getActivity(), R.layout.view_loading, null));

        if (contentLayout != null) {
            contentLayout.showLoading();
        }

        mEdt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent it = new Intent(getActivity(), SearchActivity.class);
                startActivity(it);
            }
        });

        load();
    }

    private void load(){
        ArticleGroupListMessage msg = new ArticleGroupListMessage();
        msg.setType(ArticleGroupVo.Type.VIDEO);

        NetApi.videoTitle(msg, new JsonCallback<TransMessage>() {

            @Override
            public void onFail(Call call, Exception e, int id) {
                contentLayout.showContent();
            }

            @Override
            public void onError(Call call, Exception e, int id) {
                contentLayout.showContent();
                ToastUtil.show("onFail" + e.getMessage());
            }

            @Override
            public void onResponse(TransMessage response, int id) {

                contentLayout.showContent();

                Gson gson = new Gson();
                ArticleGroupListMessage msg = gson.fromJson(response.getMessage(), ArticleGroupListMessage.class);

                if (StringUtil.equalsIgnoreCase(TransMessage.SuccessCode, msg.getCode())) {
                    List<ArticleGroupVo> groupList = msg.getGroupList();
                    if (groupList != null && groupList.size() > 0) {
                        fragmentList.clear();
                        String[] array = new String[groupList.size()];
                        for (int i = 0; i < groupList.size(); i++) {
                            ArticleGroupVo vo = groupList.get(i);
                            VideoTeamFragment fragment = new VideoTeamFragment();
                            Bundle bundle = new Bundle();
                            bundle.putLong("data",vo.getId());
                            fragment.setArguments(bundle);
                            fragmentList.add(fragment);
                            array[i] = vo.getName();
                        }

                        if (adapter == null) {
                            adapter = new XFragmentAdapter(getChildFragmentManager(), fragmentList, array);
                        }

                        pager.setAdapter(adapter);

                        tabs.setViewPager(pager);
                        tabs.setAdaptive(true);
                    }
                } else {
                }
            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_video;
    }


    @Override
    public void onPause() {
        JCVideoPlayer.releaseAllVideos();
        super.onPause();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

        if (isVisibleToUser) {
            //相当于Fragment的onResume，为true时，Fragment已经可见
        } else {
            //相当于Fragment的onPause，为false时，Fragment不可见
            JCVideoPlayer.releaseAllVideos();
        }
    }
}
